#include <stdio.h>
#include <unistd.h>
#include <math.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "iot_gpio_ex.h"
#include "iot_adc.h"
#include "iot_pwm.h"
#include"lwip/netifapi.h"
#include "lwip/api_shell.h"
#include "lwip/sockets.h"
#include "WifiSta.h"
#include "Led.h"
#define SSID "Nice"
#define Key "12312312"
#define TCP_IP "192.168.101.231"
#define TCP_PORT 5552
#define SENDMSG_HEAD "B"
#define BEEP_Freq 2442
#define beep_GPIO 2
#define adc_GPIO 13

float flame_adc;
char sendBuf[256] = {0};
char flame_str[20] = {0};
int hvalue = 80;
void TASK1();
void TASK2();
float GetMq7(void);
void Beep_Init();
void Beep_Set(unsigned short duty);
void Beep_Stop();
void Adc_Init();
static void Flame(void)
{
    printf("ADC Test!\n");
    osThreadAttr_t attr;
    Adc_Init();
    Beep_Init();
    Led_Init();
    attr.name = "AdcGpioTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 1024 * 5;
    attr.priority = osPriorityNormal;
    osThreadNew(TASK1,NULL,&attr);
    attr.name = "Task2";
    osThreadNew(TASK2,NULL,&attr);
}
void TASK1(void){
    unsigned short value;
    sleep(5);
    while (1) {
        flame_adc = GetMq7();
        if(flame_adc > hvalue){
            Led_On();
            Beep_Set(20);
        }else {
            Beep_Stop();
            Led_Off();
        }
        printf("%f",flame_adc);
        sprintf(flame_str,"%f",flame_adc);
        osDelay(100); // 延时1s
    }
}
void TASK2(void){
    struct sockaddr_in send_addr={0};
    //WIFI 连接
    ip4_addr_t ip = WifiConnect(SSID,Key);
    sleep(2);

    //创建socket
    int socketfd = socket(AF_INET,SOCK_STREAM,0);
    if(socketfd == -1)
    {
        printf("socket create failed\n");
        return -1;
    }
    printf("socket create ok\n");

    send_addr.sin_family = AF_INET;
    send_addr.sin_addr.s_addr = inet_addr(TCP_IP);
    send_addr.sin_port = htons(TCP_PORT);
    int ret = connect(socketfd,(struct sockaddr *)&send_addr,sizeof(send_addr));
    if (ret != 0)
    {
        printf("socket connect failed is %d\n",ret);
        closesocket(socketfd);
        return -1;
    }
    printf("socket connect ok\n");
    //连接服务器后发送和接收数据
    char str[20] = {0};
    int count = 0;
    while (1)
    {
      
      if(count < 1){//发送两次。每次延时2s
        strcpy(sendBuf, SENDMSG_HEAD);  
        sprintf(str, "%d",ip);
        strcat(sendBuf,str);
        count ++;
      }else{
        strcpy(sendBuf, str);
        strcat(sendBuf,flame_str);
      }
      ret = send(socketfd,sendBuf,strlen(sendBuf)+1,0);
      if (ret == -1)
        {
            printf("socket send failed is %d\n",ret);
            break;
        }
        printf("socket send ok\n");
        sleep(2);
    }
    closesocket(socketfd);
    return 0;
}

float GetMq7(void){
    unsigned short data;
    IoTAdcRead(6, &data, IOT_ADC_EQU_MODEL_8, IOT_ADC_CUR_BAIS_DEFAULT, 0xFF);
    float flame = (float)data * 1.8 * 4 / 4096;
    float s;
    s = (5-flame) / flame * 0.5;
    flame = pow(11.5428 * 6.64 / s,0.6549f) * 10;
    return flame;
}
void Adc_Init(){
    IoTGpioInit(adc_GPIO);
    IoTGpioSetFunc(adc_GPIO,IOT_GPIO_FUNC_GPIO_13_GPIO);
    IoTGpioSetDir(adc_GPIO,IOT_GPIO_DIR_IN);
    IoTGpioSetPull(adc_GPIO,1);
}
void Beep_Init(){
    IoTGpioInit(beep_GPIO);
    IoTGpioSetFunc(beep_GPIO,IOT_GPIO_FUNC_GPIO_2_PWM2_OUT);
    IoTGpioSetDir(beep_GPIO,IOT_GPIO_DIR_OUT);
    IoTPwmInit(2);
}
void Beep_Set(unsigned short duty){
     IoTPwmStart(2, duty, BEEP_Freq);
}
void Beep_Stop(){
    IoTPwmStop(2);
}
APP_FEATURE_INIT(Flame);